﻿using BLL;
using Model;
using System;
using Thksoft.Modal.Page;

namespace Thksoft.Modal
{
    public partial class EditCustomerBank : FormPage
    {
        private Sys_BankBLL bankBLL = new Sys_BankBLL();
        private Yx_CustomerBLL customerBLL = new Yx_CustomerBLL();
        private Yx_CustomerBankBLL customerBankBLL = new Yx_CustomerBankBLL();

        protected override void BindPage()
        {
            BindBank();
            if (FORM_IFEDIT)
            {
                var entity = customerBankBLL.Find(FORM_KEY);
                var cusomer = customerBLL.Find(entity.N_CUSTOMER_ID);

                FORM_CUSTOMER_NAME.Text = cusomer.C_NAME;
                //FORM_CUSTOMER_NUMBER.Text = cusomer.C_CUSTOMER_NUMBER;
                FORM_CUSTOMER_ID.Value = entity.N_CUSTOMER_ID.ToString();
                FORM_NUMBER.Text = entity.C_NUMBER;
                FORM_REAL_NAME.Text = entity.C_REALNAME;
                FORM_BANK_ID.SelectedValue = entity.N_BANK_ID.ToString();
            }
            else
            {
            }
        }

        protected override void SaveClick(object sender, EventArgs e)
        {
            if (FORM_CUSTOMER_ID.Value == "")
            {
                MessageBox.Show(this, "请选择客户");
                return;
            }
            if (FORM_NUMBER.Text.Trim() == "")
            {
                MessageBox.Show(this, "请输入开户账户");
                return;
            }
            if (FORM_REAL_NAME.Text.Trim() == "")
            {
                MessageBox.Show(this, "请输入开户姓名");
                return;
            }

            if (FORM_IFEDIT)
            {
                #region 更新

                var entity = customerBankBLL.Find(FORM_KEY);
                entity.N_CUSTOMER_ID = Convert.ToDecimal(FORM_CUSTOMER_ID.Value);
                entity.C_NUMBER = FORM_NUMBER.Text.Trim();
                entity.C_REALNAME = FORM_REAL_NAME.Text.Trim();
                entity.D_OPERATOR_DATE = DateTime.Now;
                entity.N_BANK_ID = Convert.ToDecimal(FORM_BANK_ID.SelectedValue);
                entity.N_OPERATOR_ID = CurrentUserID;
                if (customerBankBLL.Update(entity))
                {
                    MessageBox.Show(this, "修改成功！");
                }
                else
                {
                    MessageBox.Show(this, "修改失败！");
                }

                #endregion 更新
            }
            else
            {
                #region 更新

                var entity = new Yx_CustomerBank();
                entity.N_CUSTOMER_ID = Convert.ToDecimal(FORM_CUSTOMER_ID.Value);
                entity.C_NUMBER = FORM_NUMBER.Text.Trim();
                entity.C_REALNAME = FORM_REAL_NAME.Text.Trim();
                entity.D_OPERATOR_DATE = DateTime.Now;
                entity.N_BANK_ID = Convert.ToDecimal(FORM_BANK_ID.SelectedValue);
                entity.N_OPERATOR_ID = CurrentUserID;
                if (customerBankBLL.Insert(entity))
                {
                    MessageBox.ShowAndRedirect(this, "新增成功！", "/Modal/EditCustomerBank.aspx?IF_EDIT=FALSE");//&ID="+entity.N_ID);
                }
                else
                {
                    MessageBox.Show(this, "新增失败！");
                }

                #endregion 更新
            }
        }

        protected override void UpdateTitle()
        {
            if (FORM_IFEDIT)
            {
                MessageBox.ResponseScript(this, "FORM_UPDATE_TITLE(\"编辑客户银行详情\");");
            }
            else
            {
                MessageBox.ResponseScript(this, "FORM_UPDATE_TITLE(\"添加客户银行详情\");");
            }
        }

        protected override void UpdateFormSize()
        {
            MessageBox.ResponseScript(this, "FORM_SIZE(960,340);");
        }

        /// <summary>
        /// 绑定银行类型
        /// </summary>
        protected void BindBank()
        {
            int count;
            var data = bankBLL.GetEntitys(null, null, 0, 1000, out count);
            FORM_BANK_ID.DataSource = data;
            FORM_BANK_ID.DataTextField = "C_NAME";
            FORM_BANK_ID.DataValueField = "N_ID";
            FORM_BANK_ID.DataBind();
        }
    }
}